package com.sctvcloud.renhe.ui.utils;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.CustomRadioGroup;
import com.ruihang.ijkplaylib.utils.PlayerUtils;
import com.ruihang.ijkplaylib.widget.AbsSreenControll;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.ruihang.ijkplaylib.widget.PlayStateParams;
import com.sctvcloud.renhe.R;

import java.lang.ref.WeakReference;

import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by rh008 on 2017/7/20.
 */

public class GSFPlayerScreenControllImp extends AbsSreenControll implements View.OnClickListener {

    protected WeakReference<AppCompatActivity> activityWeak;
    protected WeakReference<GPlayerView> playerWeak;

    protected ViewGroup volumeBox;
    protected ViewGroup brightBox;
    protected ViewGroup fastForwardBox;

    protected ViewGroup loadBox;

    protected ImageView volumeIcon;
    protected ImageView brightIcon;
    protected TextView volumeTx;
    protected TextView brightTx;

    protected TextView fastForwardAll;
    protected TextView fastForwardTarget;
    protected TextView fastForward;

    protected ProgressBar progressBar;
    protected TextView speed;

    protected boolean isShowSpeed = true;

    protected CustomFontTextView toast;
    protected ViewGroup toastLay;
    protected ImageView playIcon;

    private Handler handler;

    private ImageView imgBack;

    private String backUrl;

    public GSFPlayerScreenControllImp(AppCompatActivity activity) {
        activityWeak = new WeakReference<AppCompatActivity>(activity);
        handler = new Handler();
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        playerWeak = new WeakReference<GPlayerView>(playerView);
        View v = inflater.inflate(R.layout.scfocus_player_touch_gestures, group, false);
        volumeBox = (ViewGroup) v.findViewById(R.id.app_video_volume_box);
        volumeIcon = (ImageView) v.findViewById(R.id.app_video_volume_icon);
        volumeTx = (TextView) v.findViewById(R.id.app_video_volume);
        brightBox = (ViewGroup) v.findViewById(R.id.app_video_brightness_box);
        brightIcon = (ImageView) v.findViewById(R.id.app_video_brightness_icon);
        brightTx = (TextView) v.findViewById(R.id.app_video_brightness);
        fastForwardBox = (ViewGroup) v.findViewById(R.id.app_video_fastForward_box);
        fastForwardAll = (TextView) v.findViewById(R.id.app_video_fastForward_all);
        fastForwardTarget = (TextView) v.findViewById(R.id.app_video_fastForward_target);
        fastForward = (TextView) v.findViewById(R.id.app_video_fastForward);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        group.addView(v, lp);
        AppCompatActivity ac = getActivityTemp();
        if (ac != null) {
            v = inflater.inflate(R.layout.loading_player_scfocus, group, false);
            progressBar = (ProgressBar) v.findViewById(R.id.loading_progressbar);
            speed = (TextView) v.findViewById(R.id.loading_speed);
            lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            group.addView(v, lp);
            loadBox = (ViewGroup) v;
//            if (isShowSpeed) {
//                speed.setVisibility(View.VISIBLE);
//            }

        }
        toastLay = (ViewGroup) inflater.inflate(R.layout.player_toast, group, false);
        toast = (CustomFontTextView) toastLay.findViewById(R.id.player_toast_text);
        playIcon = (ImageView) toastLay.findViewById(R.id.player_toast_icon);
        playIcon.setOnClickListener(this);
        imgBack = new ImageView(getActivityTemp());
        ViewGroup.LayoutParams ilp = new CustomRadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        group.addView(imgBack, ilp);
        lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        group.addView(toastLay, lp);
        toastLay.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(backUrl)) {
            GlideUtil.getGlid(getActivityTemp(), backUrl).into(imgBack);
        }
    }

    @Override
    public boolean isVolumeArea(float x, int screenWidth) {
        return x > screenWidth * 0.5f;
    }

    @Override
    public void onBrightnessSlide(float persent) {
        volumeBox.setVisibility(View.GONE);
        brightBox.setVisibility(View.VISIBLE);
        brightTx.setText(((int) (persent * 100)) + "%");
    }

    @Override
    public void onVolumeSlide(int i) {
        String s = i + "%";
        if (i == 0) {
            s = "off";
        }
        volumeIcon.setImageResource(i == 0 ? R.drawable.simple_player_volume_off_white_36dp : R.drawable.simple_player_volume_up_white_36dp);
        brightBox.setVisibility(View.GONE);
        volumeBox.setVisibility(View.VISIBLE);
        volumeTx.setText(s);
    }

    @Override
    public void onProgressSlide(long delta, long duration, long pos) {
        int showDelta = (int) delta / 1000;
        if (showDelta != 0) {
            fastForwardBox.setVisibility(View.VISIBLE);
            String text = showDelta > 0 ? ("+" + showDelta) : "" + showDelta;
            fastForward.setText(text + "s");
            fastForwardTarget.setText(PlayerUtils.generateTime(pos) + "/");
            fastForwardAll.setText(PlayerUtils.generateTime(duration));
        }
    }

    @Override
    public void showStatus(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        toastLay.setVisibility(View.VISIBLE);
        playIcon.setVisibility(View.VISIBLE);
        toast.setVisibility(View.VISIBLE);
        toast.setText(text);
    }

    @Override
    public void onProgressChanged(boolean isShow) {
        if (loadBox != null) {
            loadBox.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
        if (toastLay != null) {
            if (isShow) {
                toastLay.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onShowSome(int mode) {
        JLog.e("gsfPlayer mode=" + mode);
    }

    @Override
    public void onSetSome(int mode, Message msg) {
        JLog.e("gsf mode=" + mode + "  msg=" + msg);
        if (msg != null) {
            handler.sendMessage(msg);
        }
        if (mode == -50) {
            if (imgBack != null) {
                imgBack.setVisibility(View.GONE);
            }
            if (toastLay != null) {
                toastLay.setVisibility(View.GONE);
            }
            onProgressChanged(false);
        }
    }

    @Override
    public void onPlayerHide(GPlayerView playerView) {
        super.onPlayerHide(playerView);
        if (toastLay != null) {
            toastLay.setVisibility(View.GONE);
        }
    }

    @Override
    public void endGesture() {
        volumeBox.setVisibility(View.GONE);
        brightBox.setVisibility(View.GONE);
        fastForwardBox.setVisibility(View.GONE);
    }

    @Override
    public boolean hasBarrage() {
        return false;
    }

    @Override
    public boolean onShowPanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        //一般情况下需要隐藏中间菜单，分辨率等
        return true;
    }

    @Override
    public void onHidePanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
    }

    @Override
    public void onPortrait() {
    }

    @Override
    public void onLandscape() {
    }

    @Override
    public void onDetach(ViewGroup group) {
    }

    @Override
    public void onDestory() {
        handler = null;
        playIcon.setOnClickListener(null);
    }

    @Override
    public Message obtainMessage(int what) {
        return handler.obtainMessage(what);
    }

    @Override
    public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
        JLog.e("GPlayerView   what=" + what + " extra=" + extra);
        if (what == PlayStateParams.MEDIA_INFO_NETWORK_BANDWIDTH || what == PlayStateParams.MEDIA_INFO_BUFFERING_BYTES_UPDATE) {
            JLog.e("dou361  ====extra=======" + extra);
            if (speed != null) {
                speed.setText(PlayerUtils.getFormatSize(extra));
            }
        }
//        else if (what==PlayStateParams.STATE_PLAYING){
//            onChangedToPlaying();
//        }else if (what==PlayStateParams.STATE_ERROR){
//            onChangedToError();
//        }
        return false;
    }

    public void setBackUrl(String backUrl) {
        this.backUrl = backUrl;
    }

    private void onChangedToPlaying() {

    }

    private void onChangedToError() {
    }

    protected AppCompatActivity getActivityTemp() {
        if (activityWeak != null) {
            return activityWeak.get();
        }
        return null;
    }

    protected GPlayerView getPlayerTemp() {
        if (playerWeak != null) {
            return playerWeak.get();
        }
        return null;
    }

    public GSFPlayerScreenControllImp setShowSpeed(boolean showSpeed) {
        boolean b = this.isShowSpeed != showSpeed;
        isShowSpeed = showSpeed;
//        if (b && speed != null) {
//            speed.setVisibility(isShowSpeed ? View.VISIBLE : View.GONE);
//        }
        return this;
    }

    @Override
    public void onClick(View v) {
        GPlayerView playerView = getPlayerTemp();
        if (playerView != null) {
            playerView.startPlay();
            toastLay.setVisibility(View.GONE);
        }
    }
}
